//
//  DDHomeTableVCViewModel.swift
//  swift微博
//
//  Created by whitcast on 16/7/14.
//  Copyright © 2016年 whitcast. All rights reserved.
//

import UIKit
//对应首页的数据源
class DDHomeTableVCViewModel: NSObject {
    //单例
    static let sharedHomeTableVCViewModel: DDHomeTableVCViewModel = DDHomeTableVCViewModel()
    //数据数组的初始化
    var infosArr: [DDStatuesViewModel]? = [DDStatuesViewModel]()

    //网络数据请求
    //参数赋值为零,后面不用不会报错
    func getDateOfWeiBo(max_id: Int64 = 0,since_id:Int64 = 0, callBack:(response: AnyObject?, error: NSError?) -> ()){
        
        let url = "https://api.weibo.com/2/statuses/friends_timeline.json"
        //获取本地数据
        let model = DDUserAccountModel.getDataFormLocal()
        
        let prama :[String : AnyObject] = [
            "access_token":(model?.access_token)!,
            "max_id": "\(max_id)",     //上啦刷新
            "since_id": "\(since_id)"  //下拉刷新
        ]
        
        let str = "\(url)"+"?"+"access_token=\((model?.access_token)!)"
        
        print(str)
        //请求网络数据
        DDHTTPSessionManagerTool.shardTool.RequsetWithAFN(.GET, url: url, pramas: prama) { (response, error) -> () in
            //回调
            callBack(response: response, error: error)
        }
    }
    
    //获取数据
    func getDateForTableViewSourse(ispulled: Bool = true ,callBack: (isSuccess: Bool)->()) {
        
        var max_id: Int64 = 0  //上啦刷新
        var since_id: Int64 = 0 //下拉刷新
        if ispulled {
            //上拉刷新
            //若是刚开始,max_id也是0,若是刷新,infosArr有值
            let model = infosArr?.last
            if model == nil {
                max_id = 0
            }else {
                max_id = (model?.statuesModel?.id) ?? 0
            }
            //请求数据,避免重复请求一条数据
            if max_id > 0 {
                max_id -= 1
            }
            DDHomeTableVCViewModel.sharedHomeTableVCViewModel.getDateOfWeiBo(max_id , since_id: since_id, callBack: { (response, error) -> () in
                if error != nil {
                    //判断是否成功
                    callBack(isSuccess: false)
                    return
                }else {
                    //取出主要数据
                    let dictArr = response!["statuses"] as! [[String: AnyObject]]
                    
                    var marr = [DDStatuesViewModel]()
                    //字典转模型
                    for dict in dictArr {
                        
                        let model = DDStatusesModel.init(dict: dict)
                        
                        let statueVm = DDStatuesViewModel.init(model: model)
                        
                        marr.append(statueVm)
                    }
                    //向数组后面擦入数据
                    self.infosArr?.appendContentsOf(marr)
                    
                    callBack(isSuccess: true)
                } 
            })
            
        }else {
            //下拉 刷新
            max_id = 0
            let model = infosArr?.first
            if model == nil {
                
                since_id = 0
                
            }else {
                
                since_id = (model?.statuesModel?.id)!
            }
            
            DDHomeTableVCViewModel.sharedHomeTableVCViewModel.getDateOfWeiBo(max_id, since_id: since_id, callBack: { (response, error) -> () in
                if error != nil {
                    //判断是否成功
                    callBack(isSuccess: false)
                    return
                }else {
                    //取出主要数据
                    let dictArr = response!["statuses"] as! [[String: AnyObject]]
                    
                    var marr = [DDStatuesViewModel]()
                    //字典转模型
                    for dict in dictArr {
                        
                        let model = DDStatusesModel.init(dict: dict)
                        
                        let statueVm = DDStatuesViewModel.init(model: model)
                        
                        marr.append(statueVm)
                    }
                    
                    //判断数组是否为空
                    if  self.infosArr?.count == 0 {
                        
                        self.infosArr?.appendContentsOf(marr)
                        
                    }else {
                        
                        self.infosArr?.insertContentsOf(marr, at: 0)

                    }
                    
                    callBack(isSuccess: true)
                } 

            })
    }
        
    }
}
